<?php

namespace app\model;

use think\Model;

class Technical extends Model
{


    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'technical';

    public static function getTitle($uuid)
    {
        $data = self::where('uuid', $uuid)->find();
        return $data->title;
    }
    public static function getcityName($uuid)
    {
        $data = self::where('uuid', $uuid)->find();
        return $data->city_name;
    }
    public static function getTel($uuid)
    {
        $data = self::where('uuid', $uuid)->find();
        return $data->tel;
    }

    public static function getInfo($uuid)
    {
        $data = self::where('uuid', $uuid)->find();
        if (!empty($data)) {
            $data = $data->toArray();
        }
        return $data;
    }

    public static function getpcarray($sid = 0)
    {
        $where['weid'] = weid();
        $where['status'] = 1;
        if ($sid > 0) {
            $where['sid'] = $sid;
        }

        $data = self::field('uuid,title')->where($where)->select()->toArray();
        $datalist = [];
        foreach ($data as $key => $vo) {
            if (!empty($vo['uuid'])) {
                $datalist[$key]['val'] = $vo['uuid'];
                $datalist[$key]['key'] = $vo['title'];
            }
        }
        return $datalist;
    }
    public static function gelist_bycity($city_name)
    {
        $where['weid'] = weid();
        $where['status'] = 1;
        $where['city_name'] = $city_name;

        $query = Technical::where($where);

        $Sort = 'id desc';

        $data = $query->order($Sort)->select()->toArray();

        return $data;
    }
    public static function getdiy_bycat($cat = 0, $count = 4, $goodsSort = "all")
    {
        $where['weid'] = weid();
        $where['status'] = 1;

        if ($cat > 0) {
            if (!empty($cat)) {
                $where['category_id'] = Category::getsonid($cat);
            }
        }

        $query = Technical::where($where);

        $Sort = 'id desc';

        $data = $query->limit($count)
            ->order($Sort)->select()->toArray();

        return $data;
    }

    public static function setIncome($order_info)
    {
        $uuids =  OrderStaff::getuuid($order_info['id']);
        if (!empty($uuids)) {
            foreach ($uuids as $key => $uuid) {
                
                $Technical = Technical::where(['uuid' => $uuid])->find();
                
                if (!empty($Technical)) {
                    $Technical = $Technical->toArray();
                }
                
                if (!empty($Technical['status'])) {
                    $percent = TechnicalLevel::getPercent($Technical['level']);
                    
                    if (empty($percent)) {
                        $percent = Config::getconfig('technical')['return_percent'];
                    }
                    //如果是商户的师傅，分佣比较需要减去商户的佣金
                    if (!empty($Technical['sid'])) {
                        $Store = Store::find($Technical['sid']);
                        if (!empty($Store)) {
                            $Store = $Store->toArray();
                            $storepercent = StoreCate::getPercent($Store['stid']);
                            
                            if (empty($storepercent)) {
                                $storepercent = Config::getconfig('store')['return_percent'];
                            }
                            
                            if (!empty($storepercent)) {
                                $percent = $percent - $storepercent;
                            }
                            
                        }
                    }
                    if ((int) $percent > 0) {
                        $count = count($uuids);
                        $income = ($order_info['total_settle'] * percent_to_num($percent)) / $count;
                        $return_percent = $percent / $count;

                        if ($income > 0 && $income < 0.01) {
                            $income = 0.01;
                        }
                        Technical::where('uuid', $uuid)->inc('income', $income)->update();
                        Technical::where('uuid', $uuid)->inc('total_income', $income)->update();
                        Technical::where('uuid', $uuid)->inc('service_times', 1)->update();

                        $incomedata['uuid'] = $uuid;
                        $incomedata['ptype'] = 1;
                        $incomedata['weid'] = weid();
                        $incomedata['order_id'] = $order_info['id'];
                        $incomedata['order_num_alias'] = $order_info['order_num_alias'];
                        $incomedata['buyer_id'] = $order_info['uid'];
                        $incomedata['income'] = $income;
                        $incomedata['return_percent'] = $return_percent;
                        $incomedata['percentremark'] = $order_info['total_settle'] . '% x' . $percent . '% / ' . $count . '%';
                        $incomedata['order_total'] = $order_info['total_settle'];
                        $incomedata['pay_time'] = $order_info['pay_time'];
                        $incomedata['create_time'] = time();
                        $incomedata['month_time'] = date('m', time());
                        $incomedata['year_time'] = date('Y', time());
                        $incomedata['description'] = '订单服务，添加佣金';
                        $incomedata['order_status_id'] = Config::getconfig()['paid_order_status_id'];
                        TechnicalIncomelog::create($incomedata);
                    }
                }
            }
        }
    }
}
